import {
    validate
} from './validate';
import {
    submit
} from './submit';
import {
    editView
} from './views/edit.view';
import{
    msg, modal
}from '../common/layer';
import {
    getSectionInfo,renderSectionView
} from './renderSectionView';

const editBtnHandler = () => {

    let selectedList = $('#table').bootstrapTable('getSelections');
    if (selectedList.length === 0) {
        msg('请选择一项编辑');
        return;
    }

    const localData = selectedList[0];
    modal('编辑', editView(localData));

    getSectionInfo(function (data) {
        renderSectionView(data,'#section',localData.section);
    });

    $('#submitBtn').click(() => {
        let data = {};
        $.each($('#form').serializeArray(), (key, val) => {
            data[val.name] = val.value;
        });
        if (!validate(data)) {
            return;
        }
        submit(GLOBAL_EditUrl, data);
    });
};

export {
    editBtnHandler
}